﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SimpleBlog.Models;
using SimpleBlog.Infrastructure.Repositories;
using System.Web.Mvc;

namespace SimpleBlog.Helpers
{
    public static class SessionHelper
    {
        public static User Authenticated
        {
            get
            {
                var actualUser = (User)HttpContext.Current.Session["AuthenticatedUser"];
                if (actualUser == null && HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    var repository = DependencyResolver.Current.GetService(typeof(IUserRepository)) as IUserRepository;
                    actualUser = repository.FindByLogin(HttpContext.Current.User.Identity.Name);
                    Authenticated = actualUser;
                }
                return actualUser;
            }
            set
            {
                HttpContext.Current.Session["AuthenticatedUser"] = value;
            }
        }
    }
}